// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var css_main = require("./account.css");
	var constant = require("constant");
	var databind = require("databind");
	var dialog = require("dialog");
	var tools = require("tools");
	var flag = false;
	var pageIndex = 1,
		pageSize = 8,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		router.init();
		inquireFinanceApplicationList();
		inquireCurrentEnterpriseDetail();
		inquireEnterpriseFinanceSummaryPageList();
		initEvent();
	};

	var initEvent = function() {
		if (base.getData("sid") == null) {
			location.href = "../login/login.html";
		}

		if ($("#headImg").attr("fileUuid") != "" && $("#headImg").attr("fileUuid") != null) {
			$("#headImgBig").attr("src", $("#headImg").attr("src"));
		}

		//添加超链接
		$(".goToApprove").attr("href", "../approve/approve_main.html");

		$("#sbarAccountMain").unbind("click").click(function() {
			//			location.href="../account/account.html";
			router.route("account/accountMain.html");
		});

		$("#sbarAccountRecord").unbind("click").click(function() {
			router.route("account/accountRecord.html");
		});

		$("#sbarAccountLetter").unbind("click").click(function() {
			router.route("account/accountLetter.html");
		});

		$("#sbarAccountApply").unbind("click").click(function() {
			router.route("account/accountRecordApply.html");
		});

		$("#accountMain .searchBtn").unbind("click").click(function(){
			var waybillNumber = $("#accountMain input[name=waybillNumber]").val();
			var caseNumber = $("#accountMain input[name=caseNumber]").val();
			var financeNumber = $("#accountMain input[name=financeNumber]").val();
			var financeContractNumber = $("#accountMain input[name=financeContractNumber]").val();
			var financeContractSerialNumber = $("#accountMain input[name=financeContractSerialNumber]").val();
			inquireFinanceApplicationList(waybillNumber, caseNumber, financeNumber, financeContractNumber,financeContractSerialNumber);
		});
	};

	//获得用户信息
	var getUserInfo = function() {
		base.fnAjax({
			action: "inquireAccountDetailByAppSessionUuid.json",
			version: "v3.0",
			async: false,
			oData: {}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					$(".mobile").html(data.datas[0].fullName);
				} else {}
			}
		});
	};

	//查询企业融资汇总信息
	var inquireEnterpriseFinanceSummaryPageList = function() {
		base.fnAjax({
			action: "inquireEnterpriseFinanceSummaryPageList.json",
			async: false,
			oData: {
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if (data.datas.length != 0) {
						databind.fillForm($(".important_info"), data.datas[0]);
						$(".amount_should .span1").html(($(".data[name='totalLoanAmount']").html() - $(".data[name='totalRepayAmount']").html()).toFixed(2));
						
						if ($(".amount_should .span1").html() < 0) {
							$(".amount_should .span1").html("0");
						}
						
						$(".amount_should .span1").html(tools.formatCurrency($(".amount_should .span1").html()));
						$(".data[name='totalLoanAmount']").html(tools.formatCurrency($(".data[name='totalLoanAmount']").html()));
						$(".data[name='totalRepayAmount']").html(tools.formatCurrency($(".data[name='totalRepayAmount']").html()));
						$(".data[name='financeAmount']").html(tools.formatCurrency($(".data[name='financeAmount']").html()));
					}
				} else {}
			}
		});
	};

	//企业查询融资信息列表
	var inquireFinanceApplicationList = function(waybillNumber, caseNumber, financeNumber,financeContractNumber,financeContractSerialNumber) {
		base.fnAjax({
			action: "inquireEnterpriseInProcessFinanceApplicationPageList.json",
			async: false,
			oData: {
				pageSize: 20,
				pageIndex: pageIndex,
				waybillNumber: waybillNumber,
				caseNumber: caseNumber,
				financeNumber:financeNumber,
				financeContractNumber:financeContractNumber,
				financeContractSerialNumber:financeContractSerialNumber
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#accountMainListTpl").html();
					var html = juicer(tpl, data);
					$("#accountMainListBody").html(html);
					$(".cancelApplication").unbind("click").click(function() {
						var financeId = $(this).parents("tr").attr("financeId");
						dialog.showConfirmDialog("取消融资", "确定要取消该笔融资申请？", function() {
							cancelFinanceApplication(financeId);
						});
					});

					$(".detailApplication").unbind("click").click(function() {
						var fi = $(this).parents("tr").attr("financeId");
						var fn = $(this).parents("tr").attr("financeNumber")
						router.route("account/accountDetail.html?financeId=" + fi + "&financeNumber=" + fn);
					});
					
					base.pageControl($("#accountMain .pagerCon"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireFinanceApplicationList();
						});
				} else {}
			}
		});
	};

	//获得用户信息
	var inquireCurrentEnterpriseDetail = function() {
		base.fnAjax({
			action: "inquireCurrentEnterpriseDetail.json",
			oData: {}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var auditState = data.datas[0].auditState;
					if (auditState == 0) {
						$(".hint").removeClass("none");
						$(".goToApprove").removeClass("none");
					} else if (auditState == 1 || auditState == 0){
						$(".hint").removeClass("none");
						if (data.datas[0].submitState) {
							$(".hint").html("您已提交资质认证，请耐心等待审核结果");
							$(".goToApprove").remove();
						} else {
							$(".hint").html("您已填写资质认证，可点击<a class='submit' href='javascript:void(0);'>这里</a>提交进入审核");
							$(".submit").unbind("click").click(function(){
								dialog.showConfirmDialog("提交资质认证", "提交后不可修改,是否确定提交资质认证？", function(){
									submitEnterprise();
								});
							});
						}
					} else if (auditState == 2 || auditState == 4) {
						$(".hint").remove();
						$(".goToApprove").remove();
					} else if (auditState == 3) {
						$(".hint").removeClass("none");
						$(".hint").html("您提交的资质不通过，请重新认证");
						$(".goToApprove").removeClass("none");
					}
				} else {}
			}
		});
	};
	
	//提交企业资质
	var submitEnterprise = function() {
		base.fnAjax({
			action: "submitEnterprise.json",
			async: false,
			oData: {
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "提交成功!");
					router.route("account/accountMain.html");
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};
	
	//取消融资申请
	var cancelFinanceApplication = function(financeId) {
		base.fnAjax({
			action: "cancelFinanceApplication.json",
			async: false,
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					//					location.href = "../account/account.html";
					router.route("account/accountMain.html");
				} else {
					base.alert(2, data.message);
				}
			}
		});
	};
	
	return {
		init: init
	};
});